package com.jilijili.controller;

import com.jilijili.enums.FileTypeEnum;
import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.grace.result.ResponseStatusEnum;
import com.jilijili.pojo.Users;
import com.jilijili.pojo.bo.UsersUpdateBo;
import com.jilijili.service.UsersService;
import com.jilijili.utils.minio.MinIOConfig;
import com.jilijili.utils.minio.MinIOUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@Api(value = "文件上传控制器", tags = "文件上传测试接口", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/fileUpload")
public class FileController {

    @Resource
    private MinIOConfig minIOConfig;

    @Resource
    private UsersService usersService;


    @ApiImplicitParams({
    })
    @ApiOperation(value = "文件上传")
    @PostMapping("/uploadImage")
    public GraceJSONResult uploadImage(MultipartFile file,
                                       @RequestParam("userId") String userId) throws Exception {
        if (file.isEmpty()) {
            return GraceJSONResult.errorMsg("视频封面为空");
        }
        String filename = file.getOriginalFilename();
        MinIOUtils.uploadFile(this.minIOConfig.getBucketName(), filename, file.getInputStream());
        String fileUri = minIOConfig.getFileHost()
                + "/"
                + minIOConfig.getBucketName()
                + "/"
                + filename;


        return GraceJSONResult.ok(fileUri);
    }

    @ApiImplicitParams({
    })
    @ApiOperation(value = "文件上传")
    @PostMapping("/modifyImage")
    public GraceJSONResult update(MultipartFile file,
                                  @RequestParam("type") Integer type,
                                  @RequestParam("userId") String userId) throws Exception {

        //TODO [1] 既不是背景图片上传也不是用户头像上传
        if (!type.equals(FileTypeEnum.BGIMG.type) && !type.equals(FileTypeEnum.FACE.type)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }

        //TODO [2] 获取文件名称,上传文件
        String filename = file.getOriginalFilename();
        MinIOUtils.uploadFile(this.minIOConfig.getBucketName(), filename, file.getInputStream());

        String fileUri = minIOConfig.getFileHost()
                + "/"
                + minIOConfig.getBucketName()
                + "/"
                + filename;

        UsersUpdateBo usersUpdateBo = UsersUpdateBo.builder().id(userId).build();
        //TODO [3] 修改用户背景图片或者头像
        if (type.equals(FileTypeEnum.BGIMG.type)) {
            usersUpdateBo.setBgImg(fileUri);
        } else {
            usersUpdateBo.setFace(fileUri);
        }
        Users users = this.usersService.updateUserInfo(usersUpdateBo);

        return GraceJSONResult.ok(users);
    }

/*    @ApiImplicitParams({
    })
    @ApiOperation(value = "文件上传测试")
    @PostMapping("/upload")
    public GraceJSONResult update(MultipartFile file) throws Exception {
        //TODO [1] 获取文件名称,上传文件
        String filename = file.getOriginalFilename();
        MinIOUtils.uploadFile(this.minIOConfig.getBucketName(),filename,file.getInputStream());

        String fileUri =  minIOConfig.getFileHost()
                +"/"
                +minIOConfig.getBucketName()
                +"/"
                +filename;
        return GraceJSONResult.ok(fileUri);
    }*/

}
